home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / finderlaunch / finderlaunch.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.1 KB  |  100 lines

  1. /*    File:        FinderLaunch.c
  2.     
  3.     Description: 
  4.              A routine for sending an open documents Apple event to the
  5.             finder.  This routine provides functionality equivalent to
  6.             selecting a document/file/application and choosing the
  7.             open command in the Finder's file menu.  
  8.  
  9.     Author:    John Montbriand
  10.  
  11.     Copyright: 
  12.             Copyright © 1999 by Apple Computer, Inc.
  13.             All rights reserved worldwide.
  14.     
  15.     Disclaimer:
  16.             You may incorporate this sample code into your applications without
  17.             restriction, though the sample code has been provided "AS IS" and the
  18.             responsibility for its operation is 100% yours.  However, what you are
  19.             not permitted to do is to redistribute the source as "DSC Sample Code"
  20.             after having made changes. If you're going to re-distribute the source,
  21.             we require that you make it clear in the source that the code was
  22.             descended from Apple Sample Code, but that you've made changes.
  23.     
  24.     Change History (most recent first):
  25.             9/13/99 created by John Montbriand
  26. */
  27.  
  28. #include "FinderLaunch.h"
  29. #include <QuickDraw.h>
  30. #include <AppleEvents.h>
  31. #include <Errors.h>
  32. #include <Files.h>
  33. #include <AERegistry.h>
  34. #include <Aliases.h>
  35.  
  36.  
  37.  
  38.  
  39. OSErr FinderLaunch(long nTargets, FSSpec *targetList) {
  40.     OSErr err;
  41.     AppleEvent theAEvent, theReply;
  42.     AEAddressDesc fndrAddress;
  43.     AEDescList targetListDesc;
  44.     OSType fndrCreator;
  45.     Boolean wasChanged;
  46.     AliasHandle targetAlias;
  47.     long index;
  48.     
  49.         /* verify parameters */
  50.     if ((nTargets == 0) || (targetList == NULL)) return paramErr;
  51.     
  52.         /* set up locals  */
  53.     AECreateDesc(typeNull, NULL, 0, &theAEvent);
  54.     AECreateDesc(typeNull, NULL, 0, &fndrAddress);
  55.     AECreateDesc(typeNull, NULL, 0, &theReply);
  56.     AECreateDesc(typeNull, NULL, 0, &targetListDesc);
  57.     targetAlias = NULL;
  58.     fndrCreator = 'MACS';
  59.     
  60.         /* create an open documents event targeting the finder */
  61.     err = AECreateDesc(typeApplSignature, (Ptr) &fndrCreator,
  62.         sizeof(fndrCreator), &fndrAddress);
  63.     if (err != noErr) goto bail;
  64.     err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments,
  65.         &fndrAddress, kAutoGenerateReturnID,
  66.         kAnyTransactionID, &theAEvent);
  67.     if (err != noErr) goto bail;
  68.     
  69.         /* create the list of files to open */
  70.     err = AECreateList(NULL, 0, false, &targetListDesc);
  71.     if (err != noErr) goto bail;
  72.     for ( index=0; index < nTargets; index++) {
  73.         if (targetAlias == NULL)
  74.             err = NewAlias(NULL, (targetList + index), &targetAlias);
  75.         else err = UpdateAlias(NULL, (targetList + index), targetAlias, &wasChanged);
  76.         if (err != noErr) goto bail;
  77.         HLock((Handle) targetAlias);
  78.         err = AEPutPtr(&targetListDesc, (index + 1), typeAlias, *targetAlias, GetHandleSize((Handle) targetAlias));
  79.         HUnlock((Handle) targetAlias);
  80.         if (err != noErr) goto bail;
  81.     }
  82.     
  83.         /* add the file list to the apple event */
  84.     err = AEPutParamDesc(&theAEvent, keyDirectObject, &targetListDesc);
  85.     if (err != noErr) goto bail;
  86.  
  87.         /* send the event to the Finder */
  88.     err = AESend(&theAEvent, &theReply, kAENoReply,
  89.         kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  90.  
  91.         /* clean up and leave */
  92. bail:
  93.     if (targetAlias != NULL) DisposeHandle((Handle) targetAlias);
  94.     AEDisposeDesc(&targetListDesc);
  95.     AEDisposeDesc(&theAEvent);
  96.     AEDisposeDesc(&fndrAddress);
  97.     AEDisposeDesc(&theReply);
  98.     return err;
  99. }
  100.